<?php
	/*****Duncan Beggs - Homework 3 - CSCE 4444****/
	/*This program will go to CNN.com and display the headlines
	    Under the categories corresponding to Tech, Entertainment and Travel
	*/


   //here we store special points in the html code that denote where the categories and headlines are located
   $html1 = "</a></h4>";
   $html2 = "<h4><a href='http://www.cnn.com/";
   $html3 = "<li data-vr-contentbox=\"\">";
   $html4 = "_self\">";

  // file_get_contents is a function that attempts to connect to the file
  //'http://cnn.com' and saves everything in a large string buffer
  $content = file_get_contents( 'http://cnn.com');
  //here we go out and grab 
  $page_length_bytes = strlen($content);
  //if the page has less than 500 characters 
  if ($page_length_bytes < 500) {
	echo "Page could not be accessed.";
	echo "</br>";
	echo "CNN.com may not be accesible or your network may be having problems.";
	echo "</br>";
	echo "Crawler was not able to load all the webpage data.";
	echo "</br>";
	}
 
	//this is for incrementing our index to the next headline in the category
	//the indexes denote places in the html buffer that are of importance to finding
	//the headlines
	function inc_next_index($html_delim, $position, $page)
	{
		$position = $position + 1;
		$new_index = stripos($page, $html_delim, $position);
		return $new_index;
	}

	//store the categories that we need to use.
	//Only three categories are used for this assignment but others are available should one choose to comment them out
	$categories = array("Tech","Entertainment", "Travel", /*"U.S.", "World", "Politics",  "Opinion", "Living", "Health" */);
	
	//loop through all categories
	foreach($categories as $myCategory)
	{
		echo "</br>";
		//put things in a nice formatted bullet point list
		echo $myCategory . ": ";
		echo "<ul>";
		
		//string to search for to find beginning of headline list
		$cat_start = $myCategory . $html1;
		
		//set up our indexes
		$startIndex = stripos($content, $cat_start) + 1;
		//this points to spot that is sure to be before start of next section
		$endIndex =  stripos($content, $html2, $startIndex);
		
		$startIndex--;//decrement so first character in Headline is not excluded
		$nextIndex = stripos($content, $html3, $startIndex);
		
		//read headline that appears after start of tag
		//this index points to the first character of the headline
		$headline_Start = stripos($content, $html4, $startIndex); 
		$headline_Start = $headline_Start + strlen("_self..>") - 1;
		
		//read headline until start of closing tag
		$headline_End = stripos($content, "<", $headline_Start); 
		//calculate the length of the headline
		$offset = $headline_End - $headline_Start;
		//with the start and length of headline, now we can get the substring
		$Headline = substr($content, $headline_Start, $offset);
		
		//output headline in a bullet point
		echo "<li>$Headline</li>";
		
		
		//now that we have printed out the first headline, we can print out the rest
		//loop continues until end of category is reached
		while ($nextIndex < $endIndex) 
		{
			echo "<li>";//start bullet
			//this code is similar to the above
			$headline_Start = stripos($content, $html4, $nextIndex); 
			$headline_Start = $headline_Start + strlen($html4);
			
			$headline_End = stripos($content, "<", $headline_Start); //read headline until start of closing tag
			$Headline = substr($content, $headline_Start, ($headline_End - $headline_Start));
			
			//increment index 
			$nextIndex = inc_next_index("<li data", $nextIndex, $content);
			
			//if we're at the end move index to the very end of the page
			if( $nextIndex== 0)
			{
				$nextIndex = $page_length_bytes;
				$nextIndex--;
			}
			
			//output headline string and finish html bullet
			echo $Headline . "</li>";
		}
		//echo $Headline;
		echo "</ul></br>";
	}

  
 ?>